using System;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class HadRopeItem : BaseItem
	{
		protected BaseItem placeItem;

		protected bool needBack;

		protected Transform originalPoint;

		protected bool isLimitLength;

		protected float maxLength;

		private float tempX;

		private float tempY;

		private float tempAngle;

		protected Vector3 newPosition;

		protected Vector3 targetPosition;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		public void SetOriginalPoint(Transform val)
		{
			originalPoint = val;
		}

		public override void OnReleaseAttachItem(BaseItem oldParent)
		{
			base.OnReleaseAttachItem(oldParent);
			if (!isDrag && oldParent != null && oldParent != placeItem)
			{
				PutBackPlaceItem();
			}
		}

		protected void LimitLength()
		{
			if (isDrag && originalPoint != null && isLimitLength && Vector2.Distance(base.transform.position, originalPoint.position) > 2f)
			{
				tempAngle = GetAngle(base.transform.position, originalPoint.position);
				tempX = originalPoint.position.x + 2f * Mathf.Cos(tempAngle * 3.14f / 180f);
				tempY = originalPoint.position.y + 2f * Mathf.Sin(tempAngle * 3.14f / 180f);
				base.transform.SetXY(tempX, tempY);
			}
		}

		private float GetAngle(Vector3 from, Vector3 to)
		{
			tempX = from.x - to.x;
			tempY = from.y - to.y;
			tempAngle = 180f / ((float)Math.PI / Mathf.Acos(tempX / Mathf.Sqrt(Mathf.Pow(tempX, 2f) + Mathf.Pow(tempY, 2f))));
			if (tempY < 0f)
			{
				tempAngle = 0f - tempAngle;
			}
			else if (tempY == 0f && tempX < 0f)
			{
				tempAngle = 180f;
			}
			return tempAngle;
		}

		protected void UpdatePutBackAnim()
		{
			if (isDrag || !needBack || !(placeItem != null))
			{
				return;
			}
			targetPosition = Vector3.zero;
			newPosition = Vector2.Lerp(base.transform.localPosition, targetPosition, Time.deltaTime * 10f);
			if (Mathf.Abs(newPosition.x - targetPosition.x) < 0.01f && Mathf.Abs(newPosition.y - targetPosition.y) < 0.01f)
			{
				base.transform.SetLocalXY(targetPosition.x, targetPosition.y);
				needBack = false;
				if (parentItem == null)
				{
					placeItem.AttachItem(this, DepthType.Front, AttachType.PlacePoint, false);
				}
			}
			else
			{
				base.transform.SetLocalXY(newPosition.x, newPosition.y);
			}
		}

		protected void InitPlaceItem()
		{
			placeItem = Singleton<Scene>.Instance.GetItemById(base.itemData.scriptParams);
			if (placeItem != null && parentItem == null)
			{
				placeItem.AttachItem(this, DepthType.Front, AttachType.PlacePoint, true);
			}
		}

		protected void PutBackPlaceItem()
		{
			moveAnimType = MoveAnimType.Static;
			if (placeItem != null)
			{
				if (parentItem != null)
				{
					parentItem.ReleaseAttachItem(this);
				}
				needBack = true;
				placeItem.AttachItem(this, DepthType.Front, AttachType.PlacePoint, false);
				SetArchiveDataPosition(new Vector2(placeItem.archiveData.x, placeItem.archiveData.y));
				depthSystem.SortBaseNode();
				PlayDragRotateAnim(true);
			}
		}
	}
}
